package io.github.talelin.latticy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.core.annotation.AdminRequired;
import io.github.talelin.core.annotation.LoginRequired;
import io.github.talelin.latticy.dto.UserRechargeDTO;
import io.github.talelin.latticy.model.UserRechargeRecordDO;
import io.github.talelin.latticy.service.UserRechargeService;
import io.github.talelin.latticy.vo.UserRechargeVO;
import io.github.talelin.latticy.vo.UnifyResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * 用户充值管理控制器
 * @author talelin
 */
@RestController
@RequestMapping("/v1/recharge")
@Validated
@CrossOrigin(origins = "*")
public class UserRechargeController {

    @Autowired
    private UserRechargeService userRechargeService;

    /**
     * 创建充值记录
     */
    @PostMapping("/create")
    @AdminRequired
    public UnifyResponseVO<UserRechargeRecordDO> createRecharge(@Valid @RequestBody UserRechargeDTO dto) {
        UserRechargeRecordDO record = userRechargeService.createRechargeRecord(dto);
        return new UnifyResponseVO<UserRechargeRecordDO>(0, record);
    }

    /**
     * 处理充值（增加用户积分）
     */
    @PostMapping("/process/{recordId}")
    @AdminRequired
    public UnifyResponseVO<UserRechargeRecordDO> processRecharge(@PathVariable @NotNull Integer recordId) {
        UserRechargeRecordDO record = userRechargeService.processRecharge(recordId);
        return new UnifyResponseVO<UserRechargeRecordDO>(0, record);
    }

    /**
     * 取消充值
     */
    @PostMapping("/cancel/{recordId}")
    @AdminRequired
    public UnifyResponseVO<UserRechargeRecordDO> cancelRecharge(
            @PathVariable @NotNull Integer recordId,
            @RequestParam String reason) {
        UserRechargeRecordDO record = userRechargeService.cancelRecharge(recordId, reason);
        return new UnifyResponseVO<UserRechargeRecordDO>(0, record);
    }

    /**
     * 获取充值记录详情
     */
    @GetMapping("/record/{recordId}")
    @LoginRequired
    public UnifyResponseVO<UserRechargeRecordDO> getRechargeRecord(@PathVariable @NotNull Integer recordId) {
        UserRechargeRecordDO record = userRechargeService.getRechargeRecord(recordId);
        return new UnifyResponseVO<UserRechargeRecordDO>(0, record);
    }

    /**
     * 获取用户充值记录列表
     */
    @GetMapping("/user/{userId}/records")
    @LoginRequired
    public UnifyResponseVO<IPage<UserRechargeVO>> getUserRechargeRecords(
            @PathVariable @NotNull Integer userId,
            @RequestParam(defaultValue = "1") @Min(1) Integer page,
            @RequestParam(defaultValue = "20") @Min(1) Integer count,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String rechargeType) {
        IPage<UserRechargeVO> records = userRechargeService.getUserRechargeRecords(page, count, userId, status, rechargeType);
        return new UnifyResponseVO<IPage<UserRechargeVO>>(0, records);
    }

    /**
     * 获取所有充值记录列表（管理员）
     */
    @GetMapping("/records")
    @AdminRequired
    public UnifyResponseVO<IPage<UserRechargeVO>> getAllRechargeRecords(
            @RequestParam(defaultValue = "1") @Min(1) Integer page,
            @RequestParam(defaultValue = "20") @Min(1) Integer count,
            @RequestParam(required = false) String username,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String rechargeType) {
        IPage<UserRechargeVO> records = userRechargeService.getAllRechargeRecords(page, count, username, status, rechargeType);
        return new UnifyResponseVO<IPage<UserRechargeVO>>(0, records);
    }

    /**
     * 获取用户充值统计
     */
    @GetMapping("/user/{userId}/stats")
    @LoginRequired
    public UnifyResponseVO<List<UserRechargeVO>> getUserRechargeStats(@PathVariable @NotNull Integer userId) {
        List<UserRechargeVO> stats = userRechargeService.getUserRechargeStats(userId);
        return new UnifyResponseVO<List<UserRechargeVO>>(0, stats);
    }

    /**
     * 批量处理充值
     */
    @PostMapping("/batch-process")
    @AdminRequired
    public UnifyResponseVO<List<UserRechargeRecordDO>> batchProcessRecharge(@RequestBody List<Integer> recordIds) {
        List<UserRechargeRecordDO> records = userRechargeService.batchProcessRecharge(recordIds);
        return new UnifyResponseVO<List<UserRechargeRecordDO>>(0, records);
    }

    /**
     * 根据积分自动上架商品
     */
    @PostMapping("/auto-listing/{userId}")
    @AdminRequired
    public UnifyResponseVO<String> autoListingByPoints(@PathVariable @NotNull Integer userId) {
        userRechargeService.autoListingByPoints(userId);
        return new UnifyResponseVO<String>(0, "自动上架处理完成");
    }

    /**
     * 批量自动上架商品
     */
    @PostMapping("/batch-auto-listing")
    @AdminRequired
    public UnifyResponseVO<String> batchAutoListingByPoints(@RequestBody List<Integer> userIds) {
        userRechargeService.batchAutoListingByPoints(userIds);
        return new UnifyResponseVO<String>(0, "批量自动上架处理完成");
    }

    /**
     * 获取用户商品统计
     */
    @GetMapping("/user/{userId}/product-stats")
    @LoginRequired
    public UnifyResponseVO<Map<String, Object>> getUserProductStats(@PathVariable @NotNull Integer userId) {
        Map<String, Object> stats = userRechargeService.getUserProductStats(userId);
        return new UnifyResponseVO<Map<String, Object>>(0, stats);
    }

    /**
     * 获取上架规则描述
     */
    @GetMapping("/listing-rule/{points}")
    @LoginRequired
    public UnifyResponseVO<String> getListingRuleDescription(@PathVariable @NotNull Integer points) {
        String description = userRechargeService.getListingRuleDescription(points);
        return new UnifyResponseVO<String>(0, description);
    }

    /**
     * 处理 OPTIONS 预检请求 - 创建充值
     */
    @RequestMapping(value = "/create", method = RequestMethod.OPTIONS)
    public void handleCreateOptions() {
        // 空方法，用于处理CORS预检请求
    }

    /**
     * 处理 OPTIONS 预检请求 - 处理充值
     */
    @RequestMapping(value = "/process/*", method = RequestMethod.OPTIONS)
    public void handleProcessOptions() {
        // 空方法，用于处理CORS预检请求
    }

    /**
     * 处理 OPTIONS 预检请求 - 取消充值
     */
    @RequestMapping(value = "/cancel/*", method = RequestMethod.OPTIONS)
    public void handleCancelOptions() {
        // 空方法，用于处理CORS预检请求
    }

    /**
     * 处理 OPTIONS 预检请求 - 获取记录
     */
    @RequestMapping(value = "/record/*", method = RequestMethod.OPTIONS)
    public void handleRecordOptions() {
        // 空方法，用于处理CORS预检请求
    }

    /**
     * 处理 OPTIONS 预检请求 - 用户记录列表
     */
    @RequestMapping(value = "/user/*/records", method = RequestMethod.OPTIONS)
    public void handleUserRecordsOptions() {
        // 空方法，用于处理CORS预检请求
    }

    /**
     * 处理 OPTIONS 预检请求 - 所有记录列表
     */
    @RequestMapping(value = "/records", method = RequestMethod.OPTIONS)
    public void handleAllRecordsOptions() {
        // 空方法，用于处理CORS预检请求
    }

    /**
     * 处理 OPTIONS 预检请求 - 用户统计
     */
    @RequestMapping(value = "/user/*/stats", method = RequestMethod.OPTIONS)
    public void handleUserStatsOptions() {
        // 空方法，用于处理CORS预检请求
    }

    /**
     * 处理 OPTIONS 预检请求 - 批量处理
     */
    @RequestMapping(value = "/batch-process", method = RequestMethod.OPTIONS)
    public void handleBatchProcessOptions() {
        // 空方法，用于处理CORS预检请求
    }

    /**
     * 处理 OPTIONS 预检请求 - 自动上架
     */
    @RequestMapping(value = "/auto-listing/*", method = RequestMethod.OPTIONS)
    public void handleAutoListingOptions() {
        // 空方法，用于处理CORS预检请求
    }

    /**
     * 处理 OPTIONS 预检请求 - 批量自动上架
     */
    @RequestMapping(value = "/batch-auto-listing", method = RequestMethod.OPTIONS)
    public void handleBatchAutoListingOptions() {
        // 空方法，用于处理CORS预检请求
    }

    /**
     * 处理 OPTIONS 预检请求 - 用户商品统计
     */
    @RequestMapping(value = "/user/*/product-stats", method = RequestMethod.OPTIONS)
    public void handleUserProductStatsOptions() {
        // 空方法，用于处理CORS预检请求
    }

    /**
     * 处理 OPTIONS 预检请求 - 上架规则描述
     */
    @RequestMapping(value = "/listing-rule/*", method = RequestMethod.OPTIONS)
    public void handleListingRuleOptions() {
        // 空方法，用于处理CORS预检请求
    }
}
